import requests
import hashlib
import json
import pymysql
import sys
#对密码进行md5加密
def MD5_login(str):
    zt_pwd = hashlib.md5()
    zt_pwd.update(str.encode(encoding="utf-8"))
    return zt_pwd.hexdigest()
#登录获取cookie
def Login(user,password):
    url = "http://117.132.9.156:8020/mobile/login_check.do"
    headers = {
        "User-Agent":"Dalvik/2.1.0 (Linux; U; Android 10; YAL-AL00 Build/HUAWEIYAL-AL00)"
    }
    data = {
    "deviceUuid" : "6e6ea11a74ccea7c",
    "appVersion" : "1.3.7",
    "j_password" : MD5_login(password),
    "devicePlatform" : "Android",
    "deviceVersion" : "10",
    "j_username" : user,
    "deviceName" : "YAL-AL00"
    }
    r =requests.post(url, headers=headers, data=data,allow_redirects=False).cookies
    cookie_t = requests.utils.dict_from_cookiejar(r)
    cookies = dict(cookie_t)
    return cookies
#获取个人信息
def Geren(cookie):
    url = "http://117.132.9.156:8020/mobile/stuReminderList.do"
    headers = {
        "User-Agent":"Dalvik/2.1.0 (Linux; U; Android 10; YAL-AL00 Build/HUAWEIYAL-AL00)"
    }
    r =requests.post(url,headers=headers,cookies=cookie)
    jd = json.loads(r.text)
    sj =jd["datas"]["userinfo"]["lastLoginDate"]
    xuehao =jd["datas"]["userinfo"]["user"]["username"]
    name =  jd["datas"]["userinfo"]["user"]["realname"]
    txt = cur.execute("select username from yml where username="+xuehao)
    if txt == 0:
        cur.execute('insert into yml (realname,username,lastLoginDate) values (%s,%s,%s)',[name,xuehao,sj])
        con.commit()
    else:
        cur.execute("update yml set lastLoginDate=\"%s\" WHERE username = %s" % (sj,xuehao))
        con.commit()
    return xuehao
#获取全部课程信息
def Kechengs(cookie,xh):
    url = "http://117.132.9.156:8020/mobile/courseList.do"
    headers = {
        "User-Agent":"Dalvik/2.1.0 (Linux; U; Android 10; YAL-AL00 Build/HUAWEIYAL-AL00)"
    }
    r =requests.post(url,headers=headers,cookies=cookie)
    jd = json.loads(r.text)
    for i in jd["datas"]["courseList"]:
        kc_name = i.get("coursename")
        ls = i.get("tutor")["realname"]
        order = i.get("order")

        enterCount,inLessonTime=Xiang(cookie,order)
        print(xh,kc_name, ls, order,enterCount,inLessonTime)
        # txt = cur.execute("select username from yml2 where username=" + xh+"and order1="+order)
        txt = cur.execute(" select username from yml2 WHERE username = \"%s\" and order1= %s" % ( xh, order))
        if txt == 0:
            cur.execute('insert into yml2 (username,coursename,realname,order1,enterCount,inLessonTime) values (%s,%s,%s,%s,%s,%s)', [xh,kc_name,ls,order,enterCount,inLessonTime])
            con.commit()
        else:
            cur.execute("update yml2 set enterCount=\"%s\",inLessonTime=\"%s\" WHERE username = \"%s\" and order1= %s" % (enterCount,inLessonTime, xh,order))
            con.commit()

#该课程详细信息
def Xiang(cookie,id):
    url = 'http://117.132.9.156:8020/mobile/viewStuStudyRecord.do'
    headers = {
        "User-Agent":"Dalvik/2.1.0 (Linux; U; Android 10; YAL-AL00 Build/HUAWEIYAL-AL00)"
    }
    data = {
        "courseId":id
    }
    r = requests.post(url, headers=headers,data=data, cookies=cookie)
    jd = json.loads(r.text)
    inLessonTime = jd["datas"]["userLessonStat"]["inLessonTime"]
    enterCount = jd["datas"]["userLessonStat"]["enterCount"]
    return enterCount,inLessonTime
if __name__ == '__main__':
    con = pymysql.connect(host='10.30.59.136',port=3306,user='root',password='123456',database='wzk1',charset='utf8')
    cur = con.cursor()
    to=sys.argv[1]
    to1=sys.argv[2]
    cookie = Login(to,to1)
    yanzheng = Geren(cookie)
    Kechengs(cookie,yanzheng)
